package com.jiuwan.gamepass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiuwan.gamepass.dao.AppDao;
import com.jiuwan.gamepass.dao.IconAppDao;
import com.jiuwan.gamepass.dto.appAndIcon.AppAndIcon;
import com.jiuwan.gamepass.dto.appAndIcon.AppAndIconFormat;
import com.jiuwan.gamepass.dto.appAndIcon.PageFormat;
import com.jiuwan.gamepass.dto.appJson.AppAllJson;
import com.jiuwan.gamepass.dto.appJson.AppJson;
import com.jiuwan.gamepass.dto.authDto.Result;
import com.jiuwan.gamepass.pojo.App;
import com.jiuwan.gamepass.pojo.IconApp;
import com.jiuwan.gamepass.service.AppService;
import com.jiuwan.gamepass.utils.MyCode;
import com.jiuwan.gamepass.utils.TimeUtils;
import com.jiuwan.gamepass.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * @author： pang-yun
 * @date： 2021-06-04 10:35
 */

@Service
@Slf4j
public class AppServiceImpl implements AppService {

    @Autowired
    private IconAppDao iconAppDao;

    @Autowired
    private AppAllJson appAllJson;


    @Autowired
    public AppDao appDao;


    @Autowired
    public AppJson appJson;


    @Override
    public AppAllJson selectApp(String appName) {

        List<App> appList = appDao.selectAllByAppName(appName);
        if (appList == null) {
            appAllJson.setCode(0);
            appAllJson.setMsg("游戏名称有误");
            return appAllJson;
        }
        appAllJson.setCode(1);
        appAllJson.setMsg("success");
        appAllJson.setApps(appList);
        return appAllJson;


    }

    @Override
    public AppJson addApp(App app) {

        String uuid = UUIDUtil.createUUID();
        app.setId(uuid);
        Date nowTime = TimeUtils.getNowTime();

        app.setCreateTime(nowTime);
        app.setUpdateTime(nowTime);
        int i = appDao.addApp(app);
        if (i == 1) {
            appJson.setCode(1);
            appJson.setMsg("success");
            appJson.setApp(app);
            return appJson;

        }
        appJson.setCode(0);
        appJson.setMsg("fail");
        return appJson;

    }


    @Override
    public AppJson deleteApp(String appName) {

        int i = appDao.deleteByAppName(appName);

        if (i == 0) {
            appJson.setCode(0);
            appJson.setMsg("删除失败");
            return appJson;
        }
        appJson.setCode(1);
        appJson.setMsg("success");
        return appJson;
    }


    @Override
    public AppJson login(String appName, String version) {
        App app = new App();
        if (appName == null || version == null) {
            appJson.setCode(0);
            appJson.setMsg("必须传入参数");
            return appJson;
        }
        app.setAppName(appName);
        app.setVersion(version);
        String uuid = UUIDUtil.createUUID();
        app.setId(uuid);

        Date nowTime = TimeUtils.getNowTime();
        app.setCreateTime(nowTime);
        app.setUpdateTime(nowTime);
        int i = appDao.addApp(app);
        if (i == 0) {
            appJson.setCode(0);
            appJson.setMsg("创建失败");
            return appJson;
        }
        appJson.setCode(1);
        appJson.setMsg("创建成功");
        appJson.setApp(app);
        return appJson;
    }

    @Override
    public AppJson loginPlus(App app) {

        App app1 = appDao.selectByPrimaryKey(app.getId());
        if (app1 == null) {
            appJson.setCode(MyCode.FAIL_CODE);
            appJson.setMsg(MyCode.FAIL_MSG + "-请先创建游戏！！");
            return appJson;
        }
        app.setCreateTime(app1.getCreateTime());
        Date date = new Date();
        app.setUpdateTime(date);
        int i = appDao.updateByPrimaryKeySelective(app);
        if (i == 0) {
            appJson.setCode(0);
            appJson.setMsg("补全信息失败");
            return appJson;
        }
        appJson.setCode(1);
        appJson.setMsg("成功");
        appJson.setApp(app);
        return appJson;
    }

    @Override
    public AppAllJson selectAll() {
        List<App> apps = appDao.selectAll();
        appAllJson.setCode(1);
        appAllJson.setMsg("success");
        appAllJson.setApps(apps);

        return appAllJson;
    }

    @Override
    public AppAllJson selectPlus(String appName) {
        List<App> apps = appDao.selectPlus(appName);
        appAllJson.setCode(1);
        appAllJson.setMsg("success");
        appAllJson.setApps(apps);

        return appAllJson;
    }

    @Override
    public AppJson deleteByID(String id) {
        int i = appDao.deleteByPrimaryKey(id);
        if (i == 0) {
            appJson.setCode(0);
            appJson.setMsg("未找到相关信息");
            return appJson;
        }
        appJson.setCode(1);
        appJson.setMsg("成功");
        return appJson;
    }


    public Result selectPage(String appName, int current, int size) {
        // 用于封装数据
        HashMap<String, Object> map = new HashMap<>();
        //分页查询
        QueryWrapper<App> wrapper = new QueryWrapper<>();
        Page<App> appPage = appDao.selectPage(new Page<>(current, size), wrapper.func(appQueryWrapper -> {
            if (appName != null) {
              //  appQueryWrapper.eq("appName", appName);
                //模糊查询
                appQueryWrapper.like("appName",appName);
            }
        }));
        //查询相关icon  并将所有 信息都放到 返回 数据传输对象中
        QueryWrapper<IconApp> iconAppQueryWrapper = new QueryWrapper<>();
        List<App> records = appPage.getRecords();
        ArrayList<AppAndIcon> newRecords = new ArrayList<>();
        records.stream().forEach(a -> {
            String id = a.getId();
            List<IconApp> iconApps = iconAppDao.selectByAppID(id);
            IconApp iconApp = null;
            if (iconApps != null && iconApps.size() != 0) {
                iconApp = iconApps.get(0);
            }
            AppAndIcon appAndIcon = AppAndIconFormat.formatIcon(a, iconApp);
            newRecords.add(appAndIcon);
        });
        Page<AppAndIcon> appAndIconPage = new Page<>();
        Page page = PageFormat.pageFormat(appPage, appAndIconPage, newRecords);
        Result ok = Result.ok();
        map.put("page",page);
        ok.setData(map);
        return ok;


    }
}
